package com.tiantian.quanminxiu.dialog;

import android.app.Activity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.api.SocketUtils;
import com.tiantian.quanminxiu.event.SocketStateEvent;
import com.tiantian.quanminxiu.presenter.NotLivePresent;
import com.tiantian.quanminxiu.utils.APPUtils;
import com.tiantian.quanminxiu.utils.DialogBase;
import com.tiantian.quanminxiu.utils.EventBusUtils;
import com.tiantian.quanminxiu.utils.UserData;
import com.tiantian.quanminxiu.view.NotLiveView;
import com.tiantian.quanminxiu.view.PhoneLookView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONObject;

import rx.functions.Action1;

/**
 * Created by admin on 2016/9/12.
 */
public class Dialog_NotLive extends DialogBase implements NotLiveView {

    //region 初始化数据
    private NotLivePresent present;
    private View vBackOut,vLoading,txtRemain,vAnchorLive1,vAnchorLive3;
    private ImageView imgUserCover1,imgUserCover3;
    private TextView txtAnchorName1,txtAnchorName3,txtAnchorContribution1,txtAnchorContribution3,txtLiveTitle1,txtLiveTitle3;
    private Activity activity ;
    private int iDisplayLength = 3;                    // 数量（分页数量）
    private PhoneLookView phoneLookView;
    //endregion

    //region 构造方法 Dialog_FanRoll(Activity activity, int themeResId, int roomId)
    public Dialog_NotLive(Activity activity, int themeResId, PhoneLookView phoneLookView) {
        super(activity, themeResId);
        this.activity = activity;
        this.phoneLookView = phoneLookView;
        setContentView(R.layout.dialog_notlive);

        InitView();

        EventBusUtils.getInstance().register(this);

        present = new NotLivePresent();
        present.attachView(this);

        UserData.Reload.Dialog_FanRoll = true;
        PageInit();
    }
    //endregion

    //region 初始化View InitView
    public void InitView() {
        vBackOut = findViewById(R.id.vBackOut);
        vLoading = findViewById(R.id.vLoading);
        txtRemain = findViewById(R.id.txtRemain);

        vAnchorLive1 = findViewById(R.id.vAnchorLive1);
        vAnchorLive3 = findViewById(R.id.vAnchorLive3);

        imgUserCover1 = (ImageView) findViewById(R.id.imgUserCover1);
        imgUserCover3 = (ImageView) findViewById(R.id.imgUserCover3);

        txtAnchorName1 = (TextView) findViewById(R.id.txtAnchorName1);
        txtAnchorName3 = (TextView) findViewById(R.id.txtAnchorName3);
        txtAnchorContribution1 = (TextView) findViewById(R.id.txtAnchorContribution1);
        txtAnchorContribution3 = (TextView) findViewById(R.id.txtAnchorContribution3);
        txtLiveTitle1 = (TextView) findViewById(R.id.txtLiveTitle1);
        txtLiveTitle3 = (TextView) findViewById(R.id.txtLiveTitle3);

        RxView.clicks(vAnchorLive1).map(View->vAnchorLive1).subscribe(OC_Botton);
        RxView.clicks(vAnchorLive3).map(View->vAnchorLive3).subscribe(OC_Botton);
        RxView.clicks(vBackOut).map(View->vBackOut).subscribe(OC_Botton);
        RxView.clicks(txtRemain).map(View->txtRemain).subscribe(OC_Botton);
    }
    //endregion

    //region PageInit
    public void PageInit() {
        present.InitAnchotList(0,3);
    }
    //endregion

    //region OC_Botton
    private Action1<View> OC_Botton = v->{
        switch (v.getId()){
            case R.id.vAnchorLive1:
                phoneLookView.switchRoom((Integer) v.getTag());
                finish();
                break;
            case R.id.vAnchorLive3:
                phoneLookView.switchRoom((Integer) v.getTag());
                finish();
                break;
            case R.id.vBackOut:
                phoneLookView.finish();
                break;
            case R.id.txtRemain:
                finish();
                break;
        }
    };
    //endregion

    //region 得到主播数据 getAnchorList(JSONArray jsonArray, int iDisplayStar)
    @Override
    public void getAnchorList(JSONArray jsonArray, int iDisplayStar) {
        vAnchorLive1.setVisibility(View.INVISIBLE);
        vAnchorLive3.setVisibility(View.INVISIBLE);
        if(jsonArray!=null){
            if (jsonArray.length()>=2)
            {
                vAnchorLive3.setVisibility(View.VISIBLE);
                JSONObject obj3 = jsonArray.optJSONObject(1);
                int User_ID3 = obj3.optInt("User_ID");
                APPUtils.displayImage(APPUtils.createUserCover(User_ID3), imgUserCover3);
                txtAnchorName3.setText(obj3.optString("NickName"));
                txtAnchorContribution3.setText(obj3.optInt("Hot_Value")+"");
                txtLiveTitle3.setText(obj3.optString("RoomTitle"));
                vAnchorLive3.setTag(User_ID3);
            }

            if (jsonArray.length()>=1)
            {
                vAnchorLive1.setVisibility(View.VISIBLE);
                JSONObject obj1 = jsonArray.optJSONObject(0);
                int User_ID1 = obj1.optInt("User_ID");
                APPUtils.displayImage(APPUtils.createUserCover(User_ID1), imgUserCover1);
                txtAnchorName1.setText(obj1.optString("NickName"));
                txtAnchorContribution1.setText(obj1.optInt("Hot_Value")+"");
                txtLiveTitle1.setText(obj1.optString("RoomTitle"));
                vAnchorLive1.setTag(User_ID1);
            }
        }
    }
    //endregion

    //region 接受socket状态消息 receiveStateSocket(SocketStateEvent event)
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receiveStateSocket(SocketStateEvent event){
        if(event.getJsonObject()==null&&event.getSocketTyep()> SocketUtils.CONNECTERROR){
            return;
        }
        JSONObject jsonObject = event.getJsonObject();
        switch (event.getSocketTyep()) {
            case SocketUtils.LIVEBROADCASTLINSENER:
                //region 主播状态开播直播监听 LIVEBROADCASTLINSENER
                String LiveType = jsonObject.optString("Type");
                if("Live".equalsIgnoreCase(LiveType)){
                    finish();
                }
                //endregion
                break;
        }
    }
    //endregion

    @Override
    protected void InitView(Activity activity) {

    }

    //region finish
    @Override
    public void finish() {
        dismiss();
    }
    //endregion

    //region 监听退出键 onKeyDown(int keyCode, KeyEvent event)
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            phoneLookView.finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    //endregion

    //region dismiss
    @Override
    public void dismiss() {
        super.dismiss();
        present.detachView();
        EventBusUtils.getInstance().unregister(this);
        phoneLookView = null;
    }
    //endregion

    //region Loading_Open Loading_Close
    @Override
    public void Loading_Open() {
        vLoading.setVisibility(View.VISIBLE);
    }

    @Override
    public void Loading_Close() {
        vLoading.setVisibility(View.GONE);
    }
    //endregion

}
